package cn.edu.zjicm.wordsnet_d.util;

import java.util.List;
import java.util.Locale;

import android.content.Context;
import cn.edu.zjicm.wordsnet_d.data.Affix;
import cn.edu.zjicm.wordsnet_d.data.MobclickAgentData;

public class StringUtil {
	// 将src中的第pos用下划线替换
	public static String hideWord(Context context, String src, int pos[], String lemma) {
		if (src == null)
			return null;
		String[] rsEng = src.split(" ");
		if (pos == null)// || pos >= rsEng.length)
			return hideWord(src, lemma); // 避免数据库错误越界，直接查单词
		else {
			for (int i = 0; i < pos.length; i++) {
				if (pos[i] >= 0 && pos[i] < rsEng.length) // 避免-1和越界的情况
					src = hideWord(src, pos[i], rsEng[pos[i]]);
				else
					MobclickAgentData.exception(context, src + "越界:" + pos[i]);
			}
			while (src.indexOf("_ _") >= 0)
				src = src.replace("_ _", "___");
			return src;
		}
	}

	// 将src中的word用下划线替换, pos表示替换第几个单词
	public static String hideWord(String src, int pos, String word) {
		if (src == null)
			return null;
		String underline = "";
		for (int i = 0; i < word.length(); i++)
			underline += "_";
		String words[] = src.split(" ");

		if (!words[pos].toLowerCase().equals(word.toLowerCase())) // 如果在src中第pos个单词不等于word
			return hideWord1(src, word);

		String string = "";

		for (int i = 0; i < words.length; i++) {
			if (i > 0)
				string += " ";
			if (i == pos)
				string += underline;
			else
				string += words[i];
		}

		return string;
	}

	// 将src中的word用下划线替换
	public static String hideWord(String src, String word) {
		if (src == null)
			return null;
		String underline = "";
		for (int i = 0; i < word.length(); i++)
			underline += "_";
		String words[] = src.split(" ");
		int i;
		for (i = 0; i < words.length; i++)
			if (words[i].toLowerCase(Locale.getDefault()).equals(word.toLowerCase()))
				break;

		if (i == words.length) // 如果在src中找不到一个独立的词等于word
			return hideWord1(src, word);

		if (i == 0) {
			words[i] = words[i] + ' ';
			underline = underline + ' ';
		} else if (i == words.length - 1) {
			words[i] = ' ' + words[i];
			underline = ' ' + underline;
		} else {
			words[i] = ' ' + words[i] + ' ';
			underline = ' ' + underline + ' ';
		}
		return src.replace(words[i], underline);
	}

	public static String hideWord1(String src, String word) {
		if (src == null)
			return null;
		int i = src.toLowerCase(Locale.getDefault()).indexOf(word.toLowerCase());
		if (i != -1) {
			int j = src.indexOf(" ", i);
			String word2 = word;
			if (j == -1)
				word2 = src.substring(i);
			else
				word2 = src.substring(i, j);
			String underline = "";
			while (underline.length() < word2.length())
				underline += "_";
			return src.replace(word2, underline);
		} else {
			return hideWord(src, word.substring(0, word.length() - 1));
		}
	}

	public static String hideAffixSense(String aff_content, String affix) {
		if (aff_content == null)
			return null;
		String[] affs = affix.split(",");
		int index = -1;
		int i;
		for (i = 0; i < affs.length; i++) {
			index = aff_content.toLowerCase(Locale.getDefault()).indexOf(affs[i].toLowerCase());
			if (index != -1)
				break;
		}
		if (index != -1) {
			index += affs[i].length();
			while (index < aff_content.length() && !isChinese(aff_content.charAt(index))) {
				index++;
			}
			String word2 = "";
			String underline = "_____";
			while (index < aff_content.length() && isChinese(aff_content.charAt(index))) {
				word2 += aff_content.charAt(index);
				// underline += "_";
				index++;
			}
			return aff_content.replaceFirst(word2, underline);
		} else {
			return aff_content;
		}
	}

	public static String hideAffixSense(String aff_content, List<Affix> affixList, int affix_id) {
		if (aff_content == null)
			return null;

		for (int j = 0; j < affixList.size(); j++) {
			String[] affs = affixList.get(j).getAffix().split(",");
			int index = -1;
			int i;
			for (i = 0; i < affs.length; i++) {
				index = aff_content.toLowerCase(Locale.getDefault()).indexOf(affs[i].toLowerCase());
				if (index != -1)
					break;
			}
			if (index != -1) {
				index += affs[i].length();
				while (index < aff_content.length() && aff_content.charAt(index) >= 'a' && aff_content.charAt(index) <= 'z')
					index++;
				String word2 = "";
				String underline = "_____";
				while (index < aff_content.length() && isChinese(aff_content.charAt(index))) {
					word2 += aff_content.charAt(index);
					// underline += "_";
					index++;
				}
				if (affixList.get(j).getAffix_id() == affix_id)
					if (word2.length() > 0)
						aff_content = aff_content.replaceFirst(word2, underline);
					else
						aff_content = aff_content.substring(0, index) + underline + aff_content.substring(index);
				else if (word2.length() > 0)
					aff_content = aff_content.replaceFirst(word2, affixList.get(j).getAff_sense());
				else
					aff_content = aff_content.substring(0, index) + affixList.get(j).getAff_sense() + aff_content.substring(index);
			}
		}

		return aff_content;
	}

	public static String hideAffixSense(String aff_content, int affix_id) {
		if (aff_content == null)
			return null;

		int index = aff_content.indexOf('→');
		if (index >= 0)
			aff_content = aff_content.substring(0, index);

		String[] affs = aff_content.split("\\+");
		String str = affs[affix_id];

		int i;
		for (i = 0; i < str.length(); i++)
			if (str.charAt(i) < 'a' || str.charAt(i) > 'z')
				break;
		String str1 = str.substring(0, i) + "_____";

		return aff_content.replaceAll(str, str1);
	}

	// 隐藏助记字符串aff_content中第affix_id个词根词缀的英文
	public static String hideAffix(String aff_content, int affix_id) {
		if (aff_content == null)
			return null;

		// int index = aff_content.indexOf('→');
		// if (index >= 0)
		// aff_content = aff_content.substring(0, index);

		String[] affs = aff_content.split("\\+");
		String str = affs[affix_id];

		String str1 = "";
		int i;
		for (i = 0; i < str.length(); i++) {
			if (str.charAt(i) < 'a' || str.charAt(i) > 'z')
				break;
			else
				str1 = str1 + "_";
		}
		str1 = str1 + str.substring(i);

		return aff_content.replaceAll(str, str1);
	}

	public static boolean isChinese(char a) {
		int v = (int) a;
		return (v >= 19968 && v <= 171941);
	}

	public static String getType(String senses) {
		String cn = senses;
		int i = cn.indexOf('.');
		int j = i - 1;
		for (; j >= 0; j--) {
			char x = cn.charAt(j);
			if (x < 'A' || x > 'z')
				break;
		}

		return cn.substring(j + 1, i);
	}

	public static String nextString(String str) {
		return str.substring(0, str.length() - 1) + (char) (str.charAt(str.length() - 1) + 1);
	}

	/**
	 * 计算字数，一个汉字=两个英文字母，一个中文标点=两个英文标点 注意：该函数的不适用于对单个字符进行计算，因为单个字符四舍五入后都是1
	 * 
	 * @param c
	 * @return
	 */
	public static int calculateLength(CharSequence c) {
		int len = 0;
		for (int i = 0; i < c.length(); i++) {
			int tmp = (int) c.charAt(i);
			if (tmp > 0 && tmp < 127) {
				len += 1;
			} else {
				len += 2;
			}
		}
		return len;
	}

	public static String subString(String str, int n) {
		int len = 0, i;
		for (i = 0; i < str.length(); i++) {
			int tmp = (int) str.charAt(i);
			if (tmp > 0 && tmp < 127) {
				len += 1;
			} else {
				len += 2;
			}
			if (len > n)
				break;
		}
		if (i == str.length())
			return str;
		else
			return str.substring(0, i) + "...";
	}

	public static String hiddenString(String nickName) {
		if (nickName.contains("@")) {
			int index = nickName.indexOf("@");
			String headStr = nickName.substring(0, index);
			String endStr = nickName.substring(index);
			if (headStr.length() > 4) {
				headStr = headStr.substring(0, 4);
				nickName = headStr + "..." + endStr;
			}
		}
		return nickName;
	}

	/**
	 * 将TextView中的字符全角化，避免自动换行
	 * 
	 * @param input
	 * @return
	 */
	public static String ToDBC(String input) {
		char[] c = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == 12288) {
				c[i] = (char) 32;
				continue;
			}
			if (c[i] > 65280 && c[i] < 65375)
				c[i] = (char) (c[i] - 65248);
		}
		return new String(c);
	}
}
